package com.ms.education.course.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ms.education.course.entity.Subject;
import com.ms.education.course.entity.vo.SubjectVo;
import com.ms.education.course.mapper.SubjectMapper;
import com.ms.education.course.service.SubjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 课程类别 服务实现类
 * </p>
 *
 * @author ms
 * @since 2023-01-15
 */
@Service
public class SubjectServiceImp extends ServiceImpl<SubjectMapper, Subject> implements SubjectService {
    @Override
    public List<SubjectVo> getCateGoryTree() {
        QueryWrapper<Subject> wrapper = new QueryWrapper<>();
        wrapper.orderByAsc("sort").orderByDesc("gmt_modified");
        List<Subject> subjects = list(wrapper);
        List<SubjectVo> res = subjects.stream().filter(subject -> subject.getParentId() == 0).map(subject -> {
            SubjectVo subjectVo = new SubjectVo();
            BeanUtils.copyProperties(subject, subjectVo);
            subjectVo.setLabel(subject.getTitle());
            subjectVo.setChildren(getSubjectChildren(subjectVo.getId(), subjects));
            return subjectVo;
        }).collect(Collectors.toList());
        return res;
    }

    private List<SubjectVo> getSubjectChildren(Integer parentId, List<Subject> subjects) {
        LinkedList<SubjectVo> children = new LinkedList<>();
        for (Subject subject : subjects) {
            if (subject.getParentId() == parentId) {
                SubjectVo child = new SubjectVo();
                BeanUtils.copyProperties(subject, child);
                child.setLabel(subject.getTitle());
                child.setChildren(getSubjectChildren(child.getId(), subjects));
                children.add(child);
            }
        }
        return children;
    }


    @Override
    public void saveSubject(SubjectVo subjectVo) {
        Subject subject = new Subject();
        BeanUtils.copyProperties(subjectVo, subject);
        subject.setTitle(subjectVo.getLabel());
        this.save(subject);
    }

    @Override
    public void deleteSubjectById(Integer id) {
        // TODO 判断结点是否被关联
        this.removeById(id);
    }

    @Override
    public void updateSubject(SubjectVo subjectVo) {
        Subject subject = new Subject();
        BeanUtils.copyProperties(subjectVo,subject);
        subject.setTitle(subjectVo.getLabel());
        this.updateById(subject);
    }

    @Override
    public void updateSubjects(List<SubjectVo> subjectVos) {
        List<Subject> subjects = subjectVos.stream().map(subjectVo -> {
            Subject subject = new Subject();
            BeanUtils.copyProperties(subjectVo, subject);
            return subject;
        }).collect(Collectors.toList());
        updateBatchById(subjects);
    }
}
